package dao;

import domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    public void insert(Student stu) {
        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = ssf.openSession(true);

            int i = sqlSession.insert("StudentMapper.insert", stu);

            System.out.println(i);


        } catch (IOException e) {
            e.printStackTrace();
        }

/*       try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "root");

            PreparedStatement ps = conn.prepareStatement("insert into student values (null,?,?)");

            ps.setString(1, stu.getName());
            ps.setString(2, stu.getPassword());

            ps.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }




    public List<Student> selectAll() {
   /*      List ss= new ArrayList();
       try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "root");

            PreparedStatement ps = conn.prepareStatement("select * from student");

            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String username = resultSet.getString("name");
                String pwd = resultSet.getString("password");
                //封装对象
                Student stu = new Student(id,username,pwd);
                ss.add(stu);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ss;*/
        List ss= new ArrayList();
        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();

            List<Student> stu = sqlSession.selectList("StudentMapper.selectAll");

            for (Student student : stu) {
                System.out.println(student);
                ss.add(student);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ss;
    }


    public void delete(String id) {
        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = ssf.openSession();

            int i = sqlSession.delete("StudentMapper.delete",id);

            sqlSession.commit();
            System.out.println(i);


        } catch (IOException e) {
            e.printStackTrace();
        }

/*        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "root");

            PreparedStatement ps = conn.prepareStatement("delete  from student where id=?");

            ps.setString(1,id);

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    public void update(String id) {
        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = ssf.openSession();

            int i = sqlSession.update("StudentMapper.update",id);

            sqlSession.commit();
            System.out.println(i);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}